package edu.hcstt.irisrecognition.matching;

import java.util.Vector;

import edu.hcstt.irisrecognition.core.Feature;

public class HammingDistance {

	public static String compare(Feature[] currentIris,
			Vector<Feature[]> irisDb, Vector<String> nameDb) {
		/*
		 * Comparison through Hamming metric
		 */
		Vector<Float> distance = new Vector<Float>();
		for (Feature[] iris : irisDb) { // iterate through DB
			float dist = 0f;
			for (int i = 0; i < iris.length; i++) { // iterate through Gabor
													// factors
				dist = dist
						+ distance(iris[i].toVector(),
								currentIris[i].toVector()); // compute distance
			}
			distance.add(dist);
			System.out.println("Hamming: " + dist);
		}

		// select shortest distance
		int shortest = 0;
		float shortDist = distance.get(0);
		for (int i = 1; i < distance.size(); i++) {
			if (distance.get(i) < shortDist) {
				shortest = i;
				shortDist = distance.get(i);
			}
		}
		return nameDb.get(shortest) + ": " + shortDist;
	}

	/**
	 * 
	 * @param a
	 *            a float[] vector containing a point
	 * @param b
	 *            a float[] vector containing another point.
	 */
	protected static float distance(float[] a, float[] b) {
		int dif = 0;
		for (int i = 0; i < a.length; i++)
			if (a[i] != b[i])
				dif++;
		return (float) (dif * 1.0 / a.length);
	}
}
